package com.spark.laboratory.controller;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.spark.laboratory.pojo.Users;
import com.spark.laboratory.pojo.vo.UserScoreVO;
import com.spark.laboratory.service.UsersService;
import com.spark.laboratory.utils.TESTOKHttp;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ScoringTableController {

    private final UsersService usersService;

    @Autowired
    public ScoringTableController(UsersService usersService) {
        this.usersService = usersService;
    }

    @ResponseBody
    @PostMapping("/getScoreAll")
    public String getDate(){
        List<Users> allData = usersService.getAllData();
        UserScoreVO userScoreVO = new UserScoreVO();
        JsonArray jsonElements = new JsonArray();

        for (Users user: allData) {
            userScoreVO.setUserClass(user.getUserClass());
            userScoreVO.setUserMajor(user.getUserMajor());
            userScoreVO.setUserName(user.getUserName());
            userScoreVO.setUserSchool(user.getUserSchool());
            userScoreVO.setUserScore(user.getUserScore());
            userScoreVO.setUserSex(user.getUserSex());
            userScoreVO.setUserStudentNumber(user.getUserStudentNumber());
            JsonObject jsonObject = new Gson().toJsonTree(userScoreVO).getAsJsonObject();
            jsonElements.add(jsonObject);
        }
        return jsonElements.toString();
    }

    /**
     * 实验成绩提交接口
     * @param value 成绩
     * @param session 用户会话
     */
    @ResponseBody
    @PostMapping("/CastingWeb/test")
    public void test(String value, HttpSession session){

        System.out.println(value);
        Integer integer = new Integer(value != null && !"".equals(value) ? value : "0");

        Object userName = session.getAttribute("userName");
        Object username_ = session.getAttribute("username_");
        System.out.println("username_ = " + username_);

        Object userId = session.getAttribute("userId");


        if (userName != null && userName != ""){
            usersService.saveExpertScore(integer);
        }

        if (userId != null && userName != null){
            try {
                TESTOKHttp.interface_2(userName.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            long endDate = System.currentTimeMillis();
            long startDate = endDate - 600000L;

            try {
                TESTOKHttp.interface_1(username_.toString(),
                        integer > 100 ? 100 : integer,
                        startDate,
                        endDate,
                        10);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            usersService.saveStudentScore(new Integer(userId.toString()), integer.toString());
        }
    }
}
